package org.dyndns.opendemogroup.optimizer.problems;

import java.util.Random;

import org.dyndns.opendemogroup.optimizer.ArrayUtil;
import org.dyndns.opendemogroup.optimizer.ICustomCoding;
import org.dyndns.opendemogroup.optimizer.IOptimizationProblem;
import org.dyndns.opendemogroup.optimizer.Member;

/**
 * Maximize sum(i = 1; i < l; x(i)) with x(i) element of {0,1}.
 */
public class OneMax implements IOptimizationProblem, ICustomCoding
{
	// TODO: parameterize this
	private int memberSize;

	private double[] parameters;

	public OneMax ( int memberSize )
	{
		this.memberSize = memberSize;
		parameters = new double[memberSize];
	}

	/**
	 * @see IOptimizationProblem#computeFitness(Random)
	 */
	public double computeFitness ( Random randomSource )
	{
		double fitness = 0;
		for ( int i = 0; i < memberSize; i++ )
		{
			fitness += parameters[i];
		}
		return fitness;
	}

	/**
	 * @see IOptimizationProblem#getGeneRange()
	 */
	public int getGeneRange ( )
	{
		return 1;
	}

	/**
	 * @see IOptimizationProblem#getMemberSize()
	 */
	public int getMemberSize ( )
	{
		return memberSize;
	}

	public void decodeFrom ( Member source )
	{
		ArrayUtil.copy ( source.genome, parameters, 0, memberSize - 1 );
	}

	public void encodeTo ( Member target )
	{
		ArrayUtil.copy ( parameters, target.genome, 0, memberSize - 1 );
	}

	public boolean isMaximizing ( )
	{
		return true;
	}

	public boolean isCombinatorialOptimization ( )
	{
		return false;
	}

	public boolean makeValid ( )
	{
		// do nothing on purpose
		return false;
	}

}
